cmake_minimum_required(VERSION 2.18)
project(hello)

add_library(libhello SHARED hello.cpp) #默认是静态库，生成动态库需要添加SHARED
add_library(libstatic static.cpp)

add_executable(sayHello useHello.cpp)
target_link_libraries(sayHello libhello libstatic) #如果库文件是动态库，就是动态链接，如果是静态库就是静态链接

# 安装命令

# Get the HOME environment variable on Unix/Linux
if(UNIX)
    if(DEFINED ENV{HOME})
        set(HOME_DIR $ENV{HOME})
    else()
        message(FATAL_ERROR "HOME environment variable is not set.")
    endif()
endif()

# Get the USERPROFILE environment variable on Windows
if(WIN32)
    if(DEFINED ENV{USERPROFILE})
        set(HOME_DIR $ENV{USERPROFILE})
    else()
        message(FATAL_ERROR "USERPROFILE environment variable is not set.")
    endif()
endif()

# 设置安装路径
set(CMAKE_INSTALL_PREFIX ${HOME_DIR}/my_dir)
install(TARGETS sayHello libhello libstatic
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
)

# PROJECT_SOURCE_DIR 表示 project命令所在的cmakelist.txt文件所在的目录
install(FILES "${PROJECT_SOURCE_DIR}/hello.h" "${PROJECT_SOURCE_DIR}/static.h"
    DESTINATION include
)
